/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Classes.UsuarioClass;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author BRUNA
 */
public class UsuarioDAO {
    private Session session;
    
    public UsuarioDAO(){
            session = Util.HibernateUtil.getSessionFactory().openSession(); 
            }
    
    public void insert(UsuarioClass usu){
        Transaction t = session.beginTransaction();
        session.save(usu);
        t.commit();
        }
    
    public void update(UsuarioClass usu){
        Transaction t = session.beginTransaction();
        session.merge(usu);
        t.commit();
        }
    
    public void delete(UsuarioClass usu){
        Transaction t = session.beginTransaction();
        session.delete(usu);
        t.commit();
        }
    
    //buscar um usuário
    public UsuarioClass findbyid(int id){
        return (UsuarioClass)session.load(UsuarioClass.class, id);
        }
    
    //Buscar todos os usuários
    public List<UsuarioClass> findall(){
        Query qu = session.createQuery("From UsuarioClass");
        return qu.list();
        }
    
    public List<UsuarioClass>findAllUsu(String usu){
        Query qu = session.createQuery("From UsuarioClass where nome like '%"+usu+"%'");
        return qu.list();
    }
    
    public List<UsuarioClass> findByHQL (String hql, List params){
        Query q = this.session.createQuery(hql);
        
        int i = 0;
        for (Object ps: params){
            q.setParameter(i++, ps);            
        }
        return q.list();
        
    }
    
    public List<UsuarioClass> BuscaSenhaELogin (String usuario, String senha) {  
        Query qu = session.createQuery("FROM UsuarioClass WHERE nome like '" + usuario + "'and senha like'" + senha + "'");  
        return qu.list();
    } 
}
